﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TbShop
{
    public class PageInfo
    {
        public PageInfo(int pageSize)
        {
            PageSize = pageSize;
            PageIndex = 1;
            HttpMethod = "get";
        }

        public int PageIndex { get; set; }
        public int TotalCount { get; set; }
        public int PageSize { get; set; }
        /// <summary>
        /// 当子视图_AjaxPaginationPartial使用该模型时，该字段指明了加载分页数据使用的JS函数，该函数应该只具备一个表示页码的参数，
        /// </summary>
        public string AjaxFunction { get; set; }
        /// <summary>
        /// 存储后端的模型中的查询参数，便于分页子视图有选择的生成页码的跳转链接
        /// </summary>
        public string CustomParam { get; set; }
        public int PageCount
        {
            get
            {
                int pcount = 1;
                if (TotalCount != 0 && PageSize != 0)
                {
                    pcount = TotalCount / PageSize;
                    if (TotalCount % PageSize != 0)
                        pcount++;
                }
                return pcount;
            }
        }

        public string Prefix { set; get; }

        public string HttpMethod { set; get; }

        public const string PI = "_pi";

        public string QueryKey
        {
            get
            {
                return this.Prefix + PI;
            }
        }

    }

    public class PageHelper<T> where T : class
    {
        public static IEnumerable<T> GetPage(IEnumerable<T> models, int pageIndex, int pageSize, out int count)
        {
            count = models.Count();
            return models.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }
    }
}
